package com.personal.kousuan;

import java.util.ArrayList;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.personal.kousuan.db.CommonData;
import com.personal.kousuan.db.CommonData.Category;
import com.personal.kousuan.db.CommonData.Semester;

public class CategoryFragment extends Fragment implements
		AdapterView.OnItemSelectedListener {

	private Spinner numberRangeSpn;
	private Spinner gradeSpn;
	private Spinner categorySpn;

	private ArrayList<Integer> semesterIds = new ArrayList<Integer>();
	private ArrayList<String> semesters = new ArrayList<String>();;
	private ArrayList<String> numbers = new ArrayList<String>();
	private ArrayList<Integer> categoryIds = new ArrayList<Integer>();;
	private ArrayList<String> categoryKeys = new ArrayList<String>();
	private ArrayList<String> categoryNames = new ArrayList<String>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.setting_tab, container, false);

		numberRangeSpn = (Spinner) view.findViewById(R.id.number_range_spn);
		gradeSpn = (Spinner) view.findViewById(R.id.grade_spn);
		categorySpn = (Spinner) view.findViewById(R.id.category_spn);

		// load Semester
		CommonData commondata = CommonData.getInstance();
		for (Semester semester : commondata.semesters.values()) {
			semesterIds.add(semester.getId());
			semesters.add(semester.getName());
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				view.getContext(), R.layout.simple_spinner_item_gravity_right,
				semesters);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		gradeSpn.setAdapter(adapter);
		gradeSpn.setOnItemSelectedListener(this);

		// initial number range
		numbers.add("20");
		numbers.add("40");
		numbers.add("60");
		numbers.add("80");
		numbers.add("100");
		ArrayAdapter<String> numberRangeAdapter = new ArrayAdapter<String>(
				view.getContext(), R.layout.simple_spinner_item_gravity_right,
				numbers);
		numberRangeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		numberRangeSpn.setAdapter(numberRangeAdapter);

		categorySpn.setOnItemSelectedListener(this);
		
		numberRangeSpn.setOnItemSelectedListener(this);

		return view;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		GlobalSetting globalSetting = (GlobalSetting)getActivity().getApplicationContext();
		if (parent == gradeSpn) {
			int semesterId = semesterIds.get(position);
			CommonData commondata = CommonData.getInstance();
			Map<String, Category> categoryMap = commondata.categorys
					.get(semesterId);
			categoryIds.clear();
			categoryKeys.clear();
			categoryNames.clear();
			for (Category category : categoryMap.values()) {
				categoryIds.add(category.getId());
				categoryKeys.add(category.getCategoryKey());
				categoryNames.add(category.getCategoryName());
			}

			globalSetting.setSemester(semesters.get(position));
			
			// refresh listview
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(
					view.getContext(),
					R.layout.simple_spinner_item_gravity_right, categoryNames);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			categorySpn.setAdapter(adapter);
		} else if (parent == categorySpn) {
			globalSetting.setCategory(categoryKeys.get(position));

		} else if (parent == numberRangeSpn){
			
			globalSetting.setNumberRange(numbers.get(position));
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		if (parent == gradeSpn) {
			categorySpn.setAdapter(null);
		}
	}
}
